﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.SystemService;
using System.Collections.ObjectModel;
namespace CommunitySystem
{
    public partial class DataDictionaryWindow : ChildWindow
    {
        SystemService.SystemServiceClient client = new SystemService.SystemServiceClient();
        DataDictionaryModel DDModel = new DataDictionaryModel();
        public ObservableCollection<DataDictionaryItemModel> Model = new ObservableCollection<DataDictionaryItemModel>();
        public DataDictionaryWindow()
        {
            InitializeComponent();
            this.Title = "新增数据字典";
        }

        /// <summary>
        /// 编辑数据字典
        /// </summary>
        /// <param name="PID"></param>
        public DataDictionaryWindow(int PID)
        {
            InitializeComponent();
            this.Title = "编辑数据字典";

            //client.GetDictionarysCompleted += new EventHandler<GetDictionarysCompletedEventArgs>(client_GetDictionarysCompleted);
            //client.GetDictionarysAsync();

            client.GetDataDictionaryCompleted += new EventHandler<GetDataDictionaryCompletedEventArgs>(client_GetDataDictionaryCompleted);
            client.GetDataDictionaryAsync(PID);


            client.GetDataDictionaryItemCompleted += new EventHandler<GetDataDictionaryItemCompletedEventArgs>(client_GetDataDictionaryItemCompleted);
            client.GetDataDictionaryItemAsync(PID);
        }

        /// <summary>
        /// 绑定子集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_GetDataDictionaryItemCompleted(object sender, GetDataDictionaryItemCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            DGDDItem.ItemsSource = e.Result;
            IList<DataDictionaryItemModel> DDIModel = (IList<DataDictionaryItemModel>)e.Result;
            foreach (var item in DDIModel)
            {
                Model.Add(item);
            }
        }

        /// <summary>
        /// 读取信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_GetDataDictionaryCompleted(object sender, GetDataDictionaryCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            DDModel = (DataDictionaryModel)e.Result[0];

            DictName.Text = DDModel.DictName;
            DictCode.Text = DDModel.DictCode;

            if (DDModel.IsDynamic == false)
                JingTai.IsChecked = true;
            else
            {
                DongTai.IsChecked = true;

                SP_JingTai.Visibility = Visibility.Collapsed;
                SP_DongTai.Visibility = Visibility.Visible;


                TBSQL.Text = DDModel.DynamicSQL;

                if (DDModel.IsTree == true)
                    CKTree.IsChecked = true;
                else
                    CKTree.IsChecked = false;


            }

        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_GetDictionarysCompleted(object sender, GetDictionarysCompletedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 保存数据字典，（父子）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            DDModel.DictName = DictName.Text;
            DDModel.DictCode = DictCode.Text;


            if (true == JingTai.IsChecked)
            {
                DDModel.IsDynamic = false;
                DDModel.DynamicSQL = "";
                DDModel.IsTree = false;
            }
            else
            {
                DDModel.IsDynamic = true;
                DDModel.DynamicSQL = TBSQL.Text;
                if (true == CKTree.IsChecked)
                    DDModel.IsTree = true;
                else
                    DDModel.IsTree = false;
            }

            DDModel.CanDelete = false;
            DDModel.IsSystem = false;
            DDModel.IsAutoLoad = false;
            

            client.SaveDataDictionaryCompleted += new EventHandler<SystemService.SaveDataDictionaryCompletedEventArgs>(client_SaveDataDictionaryCompleted);
            client.SaveDataDictionaryAsync(DDModel,Model);
            this.DialogResult = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_SaveDataDictionaryCompleted(object sender, SystemService.SaveDataDictionaryCompletedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }




        /// <summary>
        /// 新增字典集合项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewItem_Click(object sender, RoutedEventArgs e)
        {
            DataDictionaryItemWindow DDItem = new DataDictionaryItemWindow();

            DDItem.Closed += new EventHandler(DDItem_Closed);

            DDItem.Show();

            
        }


        //将子项目添加到实体中
        void DDItem_Closed(object sender, EventArgs e)
        {
            DataDictionaryItemWindow DDItem = (DataDictionaryItemWindow)sender;
            if (DDItem.DialogResult == true)
            {
                Model.Add(DDItem.DDIModel);
            }

            DGDDItem.ItemsSource = Model;
        }

        /// <summary>
        /// 删除字典集合项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            if (DGDDItem.SelectedItems.Count > 0)
            {
                DataDictionaryItemModel DDModel = (DataDictionaryItemModel)DGDDItem.SelectedItems[0];
                Model.Remove(DDModel);
                DGDDItem.ItemsSource = Model;
                if (DDModel.Id > 0)
                {
                    client.DeleteDataDictionaryItemCompleted += new EventHandler<DeleteDataDictionaryItemCompletedEventArgs>(client_DeleteDataDictionaryItemCompleted);
                    client.DeleteDataDictionaryItemAsync(DDModel.Id);
                }
            }
        }

        /// <summary>
        /// 删除成功
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_DeleteDataDictionaryItemCompleted(object sender, DeleteDataDictionaryItemCompletedEventArgs e)
        {
            //MessageBox.Show("删")
        }


        /// <summary>
        /// 静态选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void JingTai_Click(object sender, RoutedEventArgs e)
        {
            if (true == JingTai.IsChecked)
            {
                SP_JingTai.Visibility = Visibility.Collapsed;
                SP_DongTai.Visibility = Visibility.Visible;
            }
            {
                SP_JingTai.Visibility = Visibility.Visible;
                SP_DongTai.Visibility = Visibility.Collapsed;
            }
        }
        /// <summary>
        /// 动态选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DongTai_Click(object sender, RoutedEventArgs e)
        {
            if (true == DongTai.IsChecked)
            {
                SP_JingTai.Visibility = Visibility.Visible;
                SP_DongTai.Visibility = Visibility.Collapsed;
            }
            {
                SP_JingTai.Visibility = Visibility.Collapsed;
                SP_DongTai.Visibility = Visibility.Visible;
            }
        }
    }
}

